const { User, Article } = require("../model");
const jwt = require("../util/jwt");
const { jwtSecret } = require("../config/config.default");

exports.getArticleList = async (req, res, next) => {
  try {
    // 支持一下查询参数
    // ?tag=AngularJS
    // ?author=jake
    // ?favorited=jake
    // ?limit=20
    // ?offset = 0
    // res.send("get /api/articles");
    const { limit = 5, offset = 0, tag, author } = req.query;
    const filter = {};
    if (tag) {
      filter.tagList = tag;
    }
    if (author) {
      const user = await User.findOne({
        username: author,
      });
      filter.author = user ? user._id : null;
    }
    const articles = await Article.find(filter)
      .skip(Number.parseInt(offset)) // 跳过多少条, 如果是字符串， 不报错， 但查询结果不对
      .limit(Number.parseInt(limit)) // 取多少条
      .sort({
        // -1倒序， 1升序
        createdAt: -1,
      });
    const articlesCount = await Article.countDocuments();
    res.status(200).json({
      articles,
      articlesCount,
    });
  } catch (error) {
    next(error);
  }
};

// feed articles
exports.feedArticles = async (req, res, next) => {
  try {
    // ?limit=20
    // ?offset = 0
    res.send("get /api/articles/feed");
  } catch (error) {
    next(error);
  }
};

// 获取单篇文章
exports.getArticle = async (req, res, next) => {
  try {
    // res.send("get /api/articles/:slug");
    const article = await Article.findById(req.params.slug).populate("author");
    if (!article) {
      return res.status(404).end();
    }
    res.status(200).json({
      article,
    });
  } catch (error) {
    next(error);
  }
};

// 创建文章
exports.createArticle = async (req, res, next) => {
  try {
    // 处理请求
    const article = new Article(req.body.article);
    article.author = req.user._id;
    // article.populate("author", "_id").execPopulate();
    await article.populate("author");
    await article.save();
    res.status(201).json({
      article,
    });
    // res.send("post /api/articles");
  } catch (error) {
    next(error);
  }
};

// 更新文章
exports.changeArticle = async (req, res, next) => {
  try {
    // res.send("put /api/articles/:slug");
    const article = req.article;
    const bodyArticle = req.body.article;
    article.title = bodyArticle.title || article.title;
    article.description = bodyArticle.description || article.description;
    article.body = bodyArticle.body || article.body;

    await article.save();
    res.status(201).json({
      article,
    });
  } catch (error) {
    next(error);
  }
};

// 删除文章
exports.removeArticle = async (req, res, next) => {
  try {
    // res.send("delete /api/articles/:slug");
    const article = req.article;
    await article.remove();
    res.status(204).end();
  } catch (error) {
    next(error);
  }
};

// 添加评论
exports.addComment = async (req, res, next) => {
  try {
    res.send("post /api/articles/:slug/comments");
  } catch (error) {
    next(error);
  }
};

// 获取评论
exports.getComments = async (req, res, next) => {
  try {
    res.send("get /api/articles/:slug/comments");
  } catch (error) {
    next(error);
  }
};

// 删除评论
exports.removeComment = async (req, res, next) => {
  try {
    res.send("delete /api/articles/:slug/comments");
  } catch (error) {
    next(error);
  }
};

// 收藏文章
exports.likeArticle = async (req, res, next) => {
  try {
    res.send("post /api/articles/:slug/favorite");
  } catch (error) {
    next(error);
  }
};

// 取消收藏文章
exports.unlikeArticle = async (req, res, next) => {
  try {
    res.send("delete /api/articles/:slug/favorite");
  } catch (error) {
    next(error);
  }
};
